package com.nq.dao;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class MyDao {

    public static JdbcTemplate template = null;

    public MyDao(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        template = jdbcTemplate;
    }

    public static BigDecimal getNowPrice(String stockg_gid) {
        BigDecimal nowprice = template.queryForObject("select ifnull(max(b.close), 0) from stock a, stock_real b where a.stock_code = b.stock_code and a.stock_gid = ?", BigDecimal.class, stockg_gid);
        return nowprice;
    }

    public static Integer getStockIdByInvestingId(int investing_id) {
        Integer r = template.queryForObject("select ifnull(max(a.id), 0) from stock a  where a.investing_id = ?", Integer.class, investing_id);
        return r;
    }

    public static Integer getInvestingIdByStockId(Integer stock_id) {
        Integer r = template.queryForObject("select ifnull(max(a.investing_id), 0) from stock a  where a.id = ?", Integer.class, stock_id);
        return r;
    }

    public static int deleteById(String table, long id) {
        int flag = template.update(String.format("delete from %s where id = ?", table), id);
        return flag;
    }

    public static boolean updateById(String table, Map<String, Object> map) {
        Integer id = MapUtil.getInt(map, "id");
        if (id == null) {
            return false;
        }
        map.remove("id");
        List<String> fields = new LinkedList<>();
        List<Object> params = new LinkedList<>();
        for (String key : map.keySet()) {
            fields.add(String.format("%s = ?", key));
            params.add(map.get(key));
        }
        params.add(id);
        String sql = String.format("update %s set %s where id = ?", table, StrUtil.join(",", fields));
        int flag = template.update(sql, params.toArray());
        return flag > 0;
    }


    public static void batchInsert(String table, List<Map<String, Object>> maps) {
        int size = maps.size();
        if (size == 0) {
            return;
        }
        if (size > 500) {
            List<List<Map<String, Object>>> parts = ListUtil.split(maps, 500);
            for (List<Map<String, Object>> part : parts) {
                batchInsert(table, part);
            }
            return;
        }
        Map<String, Object> item = maps.get(0);
        List<String> a = new LinkedList<>();
        List<String> b = new LinkedList<>();
        for (String key : item.keySet()) {
            a.add(key);
            b.add("?");
        }
        int fields = a.size();
        String sql = String.format("replace into %s (%s) values (%s)", table, StrUtil.join(",", a), StrUtil.join(",", b));
        List<Object[]> datas = new LinkedList<>();
        for (Map<String, Object> map : maps) {
            Object[] data = new Object[fields];
            for (int i = 0; i < fields; i++) {
                data[i] = map.get(a.get(i));
            }
            datas.add(data);
        }
        template.batchUpdate(sql, datas);
    }

}
